﻿/// <reference path="../jquery-2.0.2.min.js" />/// <reference path="../MJ_JSComment.js" />

/// <reference path="../commen_js/jquery.cookie.min.js" />




function getdata(indexpage, indexwhere) {
    poststr = "{\"type\":\"get_index\",\"pagecurrent\":\"" + indexpage + "\",\"indexwhere\":\"" + indexwhere + "\"}";
    mj_js_ajax("json", poststr, showlist);

}
var xuhao = 1;
var change_color = 1;
var changege_input = 1;
function showlist(data) {
    if (data.result == "ok")  {
        var record_count1 = data.record_count;
        record_count = record_count1 == 0 ? 1 : record_count1;
        var page_curent = data.page_curent;
        var datatable = data.table;
       
        var str = "";
        var table = document.getElementById("tbodyList");
        $("#tbodyList").empty();
        if (datatable.length != 0) {
        for (var i = 0; i < datatable.length; i++) {
            var tr = document.createElement("tr");
            if (change_color % 2 == 0) {
                tr.className = "bg-info";
                change_color++;
            }
            else {
                change_color++;
            }
            var td2 = document.createElement("td");
            
            td2.innerHTML = datatable[i].brand.replace("A套餐", "").replace("B套餐", "").replace("C套餐", "").replace("D套餐", ""); ;
            var td3 = document.createElement("td");
            td3.innerHTML = datatable[i].Louhao;
            var td4 = document.createElement("td");
            td4.innerHTML = datatable[i].sushe_number;
            var td5 = document.createElement("td");
            td5.innerHTML = datatable[i].shengyu_tongshu == null ? "" : datatable[i].shengyu_tongshu;
            var td6 = document.createElement("td");
            td6.innerHTML = datatable[i].songshui_tongshu == null ? "" : datatable[i].songshui_tongshu;
            var td7 = document.createElement("td");
            td7.setAttribute("contenteditable", "true");
            td7.id = datatable[i].id + "input";
            td7.innerHTML = datatable[i].huishou_tongshu == null || datatable[i].huishou_tongshu == "" ? "" : datatable[i].huishou_tongshu;
           
            var td8 = document.createElement("td");
            td8.innerHTML = datatable[i].qian_songshui == null ? "" : datatable[i].qian_songshui;
            var td9 = document.createElement("td");
            td9.innerHTML = datatable[i].ask_watertime.substring(0, datatable[i].ask_watertime.indexOf(" "));
            var td10 = document.createElement("td");
            td10.setAttribute("contenteditable", "true");
            var input_ = document.createElement("input");
            if (changege_input % 2 == 0) {
                tr.className = "bg-info";
                input_.id = "border_none1";
                changege_input++;
            }
            else {
                input_.id = "border_none";
                changege_input++;
            }
           
            input_.value = datatable[i].beizu == null ? "" : datatable[i].beizu;
            td10.appendChild(input_);
            var td11 = document.createElement("td");

            var select = document.createElement("select");
            select.id = datatable[i].id + "select";
            select.className = "select_";
            var option = document.createElement("option");
            option.setAttribute("value", "yisong");
            option.innerHTML = "未送";
            var option1 = document.createElement("option");
            option1.setAttribute("value", "weisong");
            option1.innerHTML = "已送";
            var option2 = document.createElement("option");
            option2.innerHTML = "在送";
            //0是已送1是未送2是在送
            var statee = $.trim(datatable[i].state);
            if (statee == "0") {
                option1.setAttribute("selected", "selected");
            }
            else if (statee == "1") {
                option.setAttribute("selected", "selected");
            }
            else if (statee == "2") {
                option2.setAttribute("selected", "selected");
            }
            select.appendChild(option);
            select.appendChild(option2);
            select.appendChild(option1);
            td11.appendChild(select);



            var td12 = document.createElement("td");

            var staff = datatable[i].songshui_gong;
            if(staff==""||staff==null){
                staff="";
            }
            td12.innerHTML = staff;
            var td13 = document.createElement("td");
            var label1 = document.createElement("input");
            label1.id = datatable[i].id;
            label1.value = "修改";
            label1.setAttribute("type", "button");
            label1.className = "btn btn-info input1";
            label1.onclick = function () {
                xiugai(this.id);

            }
            var label2 = document.createElement("input");
            label2.id = datatable[i].id;
            label2.value = "删除";
            label2.setAttribute("type", "button");
            label2.className = "btn btn-default input2";
            label2.onclick = function () {
                delect(this.id);
            }
            td13.appendChild(label1);
            td13.appendChild(label2);
            
            tr.appendChild(td2);
            tr.appendChild(td3);
            tr.appendChild(td4);
            tr.appendChild(td5);
            tr.appendChild(td6);
            tr.appendChild(td7);
            tr.appendChild(td8);
            tr.appendChild(td9);
            tr.appendChild(td10);
            tr.appendChild(td11);
            tr.appendChild(td12);
            tr.appendChild(td13);
            table.appendChild(tr);
        }
        }
        else {
            $("#tbodyList").empty();
        }
        //下面进行的是分页
        $("#record_shu").text(" 总记录数" + record_count1);
        if (record_count % 10 != 0) {
            page_shu = record_count / 10 + 1;
            page_shu = parseInt(page_shu)
        }
        else {
            page_shu = record_count / 10;
           
        }
  

        var options = {
           // currentPage: page_curent,
            totalPages: page_shu,
            numberOfPages: 5,
            itemTexts: function (type, page, current) {
                switch (type) {
                    case "first":
                        return "第一页";
                    case "prev":
                        return "上一页";
                    case "next":
                        return "下一页";
                    case "last":
                        return "最后一页";
                    case "page":
                        return page;
                }
            },
            onPageClicked: function (event, originalEvent, type, page) {
                if (page == 1) {
                    xuhao = 1;
                }
                else {
                    var n = page - 1;
                    xuhao = n * 10 + 1;
                }
                var quyu = $.trim($("#dateboxs").text());
                var louhao = $.trim($("#building").val());
                var sushe_hoa = $.trim($("#dorm").val());
                var begintime = $.trim($("#begintime").val());
                var endtime = $.trim($("#endtime").val());
                var songshuigong = $.trim($("#songshuigong").text());
                var zhuangtai = $.trim($("#list").text());
                var arrey = quyu + "|" + louhao + "|" + sushe_hoa + "|" + begintime + "|" + endtime + "|" + songshuigong + "|" + zhuangtai;
                getdata(page, arrey);
            }
        }
        $('#example').bootstrapPaginator(options);
   
}

    else {
        layer.alert(data.msg, {
            icon: 1,
            skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
        })
   // alert(data.msg);

}

}

var xiugai_id = "1";


function xiugai(id) {
    $.cookie("cookie_xiugai_id", id);
    xiugai_id = id;
    var state = $("#" + id + "select").find("option:selected").text();
    $("#content_two").text("确定修改这条记录为"+state+"？");
    $("#LoginBox_two").fadeIn("fast");
    $("#LoginBox_two").css({ display: 'block' });
}

function sendxiugai_data() {
   var id = xiugai_id;
    var huishou_tongshu1 = $.trim($("#" + id + "input").text());
    huishou_tongshu = huishou_tongshu1 == 0 ? "" : huishou_tongshu1;
    var state = $("#" + id + "select").find("option:selected").text();
    if (state == "在送") {

    }
    else if (state == "未送" && huishou_tongshu != "") {
        layer_alert("未送时候不能有回收桶数！");
        return false;
    }
    else if (state == "已送" && huishou_tongshu == "") {
        huishou_tongshu = 0;
    }
    poststr = "{\"type\":\"xiugai_index\",\"xiugai_content\":\"" + id + "|" + huishou_tongshu + "|" + state + "\"}";
    mj_js_ajax("json", poststr, show_state);
}

function show_state(data) {
    if (data.result == "ok") {
        try {
            layer.alert(data.msg, {
                icon: 1,
                skin: 'layer-ext-moon'
            })
        }
        catch (e) { }

    }
    else {
        alert(data.msg);
    }

}

function search() {
    xuhao = 1;
    var quyu = $.trim($("#dateboxs").text());
    var louhao = $.trim($("#building").val());
    var sushe_hoa = $.trim($("#dorm").val());
    var begintime = $.trim($("#begintime").val());
    var endtime = $.trim($("#endtime").val());
    var songshuigong = $.trim($("#songshuigong").text());
    var zhuangtai = $.trim($("#list").text());
    var arrey = quyu + "|" + louhao + "|" + sushe_hoa + "|" + begintime + "|" + endtime + "|" + songshuigong + "|" + zhuangtai;
    getdata("1", arrey);
}
var delete_;
function delect(id) {
    if ($.cookie("manager_name") != "yidashuihang") {
        alert("权限不够，不能删除数据！");
        return;
    }
    delete_ = id;
    $("#content_two").text("确定要删除这条记录？");
    $("#LoginBox_two").fadeIn("fast");
    $("#LoginBox_two").css({ display: 'block' });
}

function yes_button() {

    var cookie_xiugai_id = $.cookie("cookie_xiugai_id");
    if (cookie_xiugai_id == xiugai_id && cookie_xiugai_id != "" && xiugai_id != "1") {
        sendxiugai_data();
        $.cookie("cookie_xiugai_id","");
        $("#LoginBox_two").fadeOut("fast");
        $("#LoginBox_two").css({ display: 'none' });
    }
    else {
        if (delete_ == null || delete_ == "") {
            alert("删除出错，");
            return;
        }
        var quyu = $.trim($("#dateboxs").text());
        var louhao = $.trim($("#building").val());
        var sushe_hoa = $.trim($("#dorm").val());
        var begintime = $.trim($("#begintime").val());
        var endtime = $.trim($("#endtime").val());
        var songshuigong = $.trim($("#songshuigong").text());
        var zhuangtai = $.trim($("#list").text());
        var arrey = quyu + "|" + louhao + "|" + sushe_hoa + "|" + begintime + "|" + endtime + "|" + songshuigong + "|" + zhuangtai;
        poststr = "{\"type\":\"index_delect\",\"dormcombolist_id\":\"" + delete_ + "\",\"pagecurrent\":\"1\",\"indexwhere\":\"" + arrey + "\"}";
        mj_js_ajax("json", poststr, showlist);
        $("#LoginBox_two").fadeOut("fast");
        $("#LoginBox_two").css({ display: 'none' });
    }
}
function no_button() {
    $("#LoginBox_two").fadeOut("fast");
    $("#LoginBox_two").css({ display: 'none' });

}
function loaddown_excel0() {
    var quyu = $.trim($("#dateboxs").text());
    var louhao = $.trim($("#building").val());
    var sushe_hoa = $.trim($("#dorm").val());
    var songshuigong = $.trim($("#songshuigong").text());
    var zhuangtai = $.trim($("#list").text());
    var arrey = quyu + "|" + louhao + "|" + sushe_hoa + "|"+ songshuigong + "|" + zhuangtai;
    poststr = "{\"type\":\"ask_water_loaddowm\",\"code\":\"" + arrey + "\"}";
    mj_js_ajax("json", poststr, loaddown_excel_result);


}

function loaddown_excel_result(data) {
    if (data.result == "ok") {
        try{
            window.open(data.down_link);
        }
        catch(e){}
    }
    else { }


}